package me.guowenlong.rxextension;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import me.guowenlong.rxextension.api.Api;
import me.guowenlong.rxextension.api.Config;
import me.guowenlong.rxextension.api.HttpLog;
import me.guowenlong.rxextension.api.RetrofitHelper;
import me.guowenlong.rxextension.bean.RequestEntity;
import me.guowenlong.rxextension.bean.ResponseEntity;
import me.guowenlong.rxextension.bean.SendCode;
import me.guowenlong.rxextension.bean.UsernameAndPassword;
import me.guowenlong.rxextension.ext.FailMap;
import me.guowenlong.rxextension.ext.TransformerUtils;
import me.guowenlong.rxextension.ext.exception.UnauthorizedException;
import me.guowenlong.transformer.core.ExtendTransformer;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Response;


public class MainActivity extends AppCompatActivity {
    private Api mApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mApi = initApi();
        test();
    }

    private Api initApi() {
        return new RetrofitHelper.Builder()
                .setBaseUrl(Config.URL_HTTP_HOST)
                .setRelease(BuildConfig.DEBUG)
                .addInterceptor(new HttpLoggingInterceptor(new HttpLog()).setLevel(HttpLoggingInterceptor.Level.BODY))
                .build()
                .create(Api.class);
    }

    private void test() {
//        ExtendTransformer<Response<ResponseEntity<?>>> baseEntityExtendTransformer = TransformerUtils.handleGlobalError(this);
//        Observable<Response<ResponseEntity<?>>> observable = Observable.error(UnauthorizedException::new);
//        observable.compose(baseEntityExtendTransformer)
//                .subscribe(new Observer<Response<ResponseEntity<?>>>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(Response<ResponseEntity<?>> response) {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
    }

    private void testApi() {
        mApi.sendCode1(new RequestEntity(new UsernameAndPassword("17600133786")))
                .compose(TransformerUtils.handleGlobalError(this))
                .map(new FailMap<>())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}